package edu.tongji.app.component;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ImageCircleView extends ImageView {
	
	private Paint paint;

	public ImageCircleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public ImageCircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ImageCircleView(Context context) {
		super(context);
		init();
	}

	public void init() {
		paint = new Paint();
	}

	@Override
	protected void onDraw(Canvas cns) {
		Drawable drawable = getDrawable();
		if (null != drawable) {
			Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
			Bitmap b = circleDraw(bitmap);
			final Rect rect1 = new Rect(0, 0, b.getWidth(), b.getHeight());
			final Rect rect2 = new Rect(0, 0, getMeasuredWidth(),
					getMeasuredHeight());
			paint.reset();
			cns.drawBitmap(b, rect1, rect2, paint);
			b.recycle();
		} else {
			super.onDraw(cns);
		}
	}

	private Bitmap circleDraw(Bitmap bitmap) {
		int r = 0;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		if (width > height)
			r = height;
		else {
			r = width;
		}
		Bitmap output = Bitmap.createBitmap(r, r, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final Rect rect = new Rect(0, 0, r, r);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(Color.WHITE);
		canvas.drawCircle(r / 2, r / 2, r / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
}
